package com.example.gkgyservice.thread;

import com.alibaba.fastjson.JSON;
import com.example.gkgyservice.domain.OpcUaServer;
import com.example.gkgyservice.domain.ServiceData;
import com.example.gkgyservice.service.impl.OpcUaSocketServiceImpl;
import lombok.SneakyThrows;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.example.gkgyservice.service.impl.OpcUaSocketServiceImpl.opcUaServerList;

/**
 * @author:xec
 * @date:2022/12/6 13:41
 **/
public class SocketThread extends Thread {

    public Socket socket;

    public SocketThread(Socket socket) {
        this.socket = socket;
    }

    static String path = System.getProperty("user.dir") + "/sgp/gkgt-server-data.sgp";

    public void run() {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            String msg = "";
            while ((msg = reader.readLine()) != null) {
//                System.out.println(msg);
                if (msg.equals("readData")) {
                    writer.write(JSON.toJSONString(opcUaServerList) + "\n");
                    writer.flush();
                }
                Map<String, List<OpcUaServer>> uidMap = opcUaServerList.parallelStream().collect(Collectors.groupingBy(OpcUaServer::getUid));
                int i = msg.indexOf("?");
                if (i >0) {
                    System.out.println(msg);
                    int flag = 0;
                    String s = msg.substring(i+1);
                    if (s != null) {
                        Thread.sleep(3000);
                        OpcUaServer opcUaServer = JSON.parseObject(s, OpcUaServer.class);
                        List<OpcUaServer> opcUaServers = new ArrayList<>();
                        for (OpcUaServer uaServer : opcUaServerList) {
                            if (opcUaServer.getUid().equals(uaServer.getUid())) {
                                List<ServiceData> nodeList = opcUaServer.getNodeList();
                                List<ServiceData> nodeList1 = uaServer.getNodeList();
                                for (ServiceData serviceData : nodeList) {
                                    for (ServiceData data : nodeList1) {
                                        if (data.getDisplayName().equals(serviceData.getDisplayName())) {
                                            if (serviceData.getNodeValue()!=null) {
                                                data.setNodeValue(serviceData.getNodeValue());
                                            }
                                        }
                                    }
                                }
                                flag = 1;
                                uaServer.setNodeList(nodeList1);
                                writer.write(JSON.toJSONString(uaServer) + "\n");
                                writer.flush();
                            }
                            opcUaServers.add(uaServer);
                        }
                        // 若是没有找到对应的就退出
                        if (flag == 1) {
                            File file = new File(path);
                            opcUaServerList = opcUaServers;
                            FileOutputStream fileOutputStream = new FileOutputStream(file);
                            fileOutputStream.write(JSON.toJSONString(opcUaServerList).getBytes());
                            fileOutputStream.close();
                        }
                    }
                }else {
                    switch (msg) {
                        case "PLC-2019-ZP-JFX":
                        case "PLC-2019-JC-XN":
                        case "PLC-2020-ZJ-XF":
                        case "PLC-2020-JC-FPX":
                        case "PLC-2020-ZP-FPX":
                        case "PLC-2020-JC-JQR":
                        case "PLC-2020-JC-JYF":
                        case "PLC-2020-JC-LF":
                        case "PLC-2020-ZJ-THM":
                        case "PLC-2020-ZP-ZC":
                        case "PLC-2019-YZ-ZFQ":
                            String s = JSON.toJSONString(uidMap.get(msg));
                            writer.write(s + "\n");
                            writer.flush();
                            break;
                        default:
                            writer.write("NO\n");
                            writer.flush();
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}


